<?php
namespace app\system\controller;

use org\util\Baksql;
use think\Db;
use think\Request;

class DatabaseController extends BaseController
{

    /**
     * 初始化
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
    }

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
        if ($this->request->isPost()) {
            $sql = trim($_POST['sql']);
            if (strtolower(substr($sql, 0, 6)) == 'select') {
                $result = Db::query($sql);
            } else {
                $num    = Db::execute($sql);
                $result = '影响记录数:[' . $num . ']';
            }
            return dump($result);
        }
        return $this->fetch();
    }

    /**
     * 数据库备份还原
     */
    public function bak()
    {
        $action = $this->request->param('action');
        $name   = $this->request->param('name');
        $sql    = new Baksql(config('database'));
        switch ($action) {
            case "backup": //备份
                return $sql->backup();
                break;
            case "dowonload": //下载
                $sql->downloadFile($name);
                break;
            case "restore": //还原
                return $sql->restore($name);
                break;
            case "del": //删除
                return $sql->delfilename($name);
                break;
            default: //获取备份文件列表
                $list = $sql->get_filelist();
                $this->assign('list', $list);
                return $this->fetch();

        }
    }

}
